/** 
 * File-Name:CommonAction.java
 *
 * Created on 2010-12-22 下午02:18:38
 * 
 * @author: Neo (neolimeng@gmail.com)
 * Software Engineering Institute, Peking University, China
 * 
 * Copyright (c) 2009, Peking University
 * 
 *
 */
package neoutil;

import org.apache.struts2.ServletActionContext;

import site.service.ArticleRepository;
import site.service.CategoryRepository;
import site.service.PaperRepository;
import site.service.ResourceRepository;
import site.service.ReviewRepository;
import site.service.UserRepository;

import com.opensymphony.xwork2.ActionSupport;

/**
 * Description:Base Action class for the Tutorial package.
 * 
 * @author: Neo (neolimeng@gmail.com) Software Engineering Institute, Peking
 *          University, China
 * @version 1.0 2010-12-22 下午02:18:38
 */
public class CommonAction extends ActionSupport {
	private static final long serialVersionUID = -4985115609408627931L;
	public static final String RELOGIN = "relogin";
	public static final String EXCEPTION = "exception";

	protected String jsonResult;

	protected String errorMessage;

	protected String returnURL;

	protected Object getSessionObject(String key) {
		return ServletActionContext.getRequest().getSession().getAttribute(key);
	}

	protected void setSessionObject(String key, Object value) {
		ServletActionContext.getRequest().getSession().setAttribute(key, value);
	}

	protected String getRequestParameter(String key) {
		try {
			String rawParam = ServletActionContext.getRequest().getParameter(key);
			if (rawParam == null || "".equals(rawParam)) {
				return null;
			} else {
				return new String(rawParam.getBytes("iso-8859-1"), "UTF-8");
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public String getJsonResult() {
		return jsonResult;
	}

	public void setJsonResult(String jsonResult) {
		this.jsonResult = jsonResult;
	}

	public String getErrorMessage() {
		return errorMessage;
	}

	public void setErrorMessage(String errorMessage) {
		this.errorMessage = errorMessage;
	}

	public String getReturnURL() {
		return returnURL;
	}

	public void setReturnURL(String returnURL) {
		this.returnURL = returnURL;
	}

	private static ArticleRepository articleRepository;

	protected ArticleRepository getArticleRepository() {
		if (articleRepository == null) {
			articleRepository = new ArticleRepository();
		}
		return articleRepository;
	}

	private static CategoryRepository categoryRepository;

	protected CategoryRepository getCategoryRepository() {
		if (categoryRepository == null) {
			categoryRepository = new CategoryRepository();
		}
		return categoryRepository;
	}

	private static ResourceRepository resourceRepository;

	protected ResourceRepository getResourceRepository() {
		if (resourceRepository == null) {
			resourceRepository = new ResourceRepository();
		}
		return resourceRepository;
	}

	private static UserRepository userRepository;

	protected UserRepository getUserRepository() {
		if (userRepository == null) {
			userRepository = new UserRepository();
		}
		return userRepository;
	}
	
	private static ReviewRepository reviewRepository;

	protected ReviewRepository getReviewRepository() {
		if (reviewRepository == null) {
			reviewRepository = new ReviewRepository();
		}
		return reviewRepository;
	}
	
	private static PaperRepository paperRepository;

	protected PaperRepository getPaperRepository() {
		if (paperRepository == null) {
			paperRepository = new PaperRepository();
		}
		return paperRepository;
	}
}
